package cn.coisini.model.common.mail.impl;

import cn.coisini.model.common.mail.MailService;
import cn.coisini.model.common.mail.property.MailProperties;
import jakarta.mail.MessagingException;
import jakarta.mail.internet.MimeMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.Date;
import java.util.Objects;

/**
 * @author xiangshaw
 * Description: 邮件服务实现类
 */
@Log4j2
@Service
@RequiredArgsConstructor
public class MailServiceImpl implements MailService {

    private final MailProperties mailProperties;
    private final JavaMailSender mailSender;

    /**
     * 发送邮件
     * @param to 邮件接收人
     * @param subject 邮件主题
     * @param content 邮件内容
     */
    @Override
    public void sendMail(String to, String subject, String content) {
        try {
            // 创建邮件对象
            SimpleMailMessage message = new SimpleMailMessage();
            // 发送人
            message.setFrom(mailProperties.getFrom());
            // 接收人
            message.setTo(to);
            // 邮件主题
            message.setSubject(subject);
            // 邮件内容
            message.setText(content);
            // 发送邮件
            mailSender.send(message);
        } catch (MailException e) {
            log.error("邮件发送失败", e);
        }
    }

    /**
     * 发送带附件的邮件
     * @param to 邮件接收人
     * @param subject 邮件主题
     * @param content 邮件内容
     * @param filePath 附件路径
     */
    @Override
    public void sendAttachmentsMail(String to, String subject, String content, String filePath) {
        // 创建MIME邮件对象
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        try {
            // 邮件是否支持附件
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
            // 发送人
            helper.setFrom(mailProperties.getFrom());
            // 接收人
            helper.setTo(to);
            // 邮件主题
            helper.setSubject(subject);
            // 邮件内容，添加true表示支持html格式
            helper.setText(content, true);
            // 附件
            FileSystemResource fileSystemResource = new FileSystemResource(new File(filePath));
            helper.addAttachment(Objects.requireNonNull(fileSystemResource.getFilename()), fileSystemResource);
            // 发送邮件
            mailSender.send(mimeMessage);
        } catch (MessagingException e) {
            log.error("邮件发送失败", e);
        }
    }

    /**
     * 发送验证码
     */
    @Override
    public void sendVerificationCode(String email, String code) {
        String content = "<html>\n" +
                "\t<body><div id=\"contentDiv\" onmouseover=\"getTop().stopPropagation(event);\" onclick=\"getTop().preSwapLink(event, 'html', 'ZC0004_vDfNJayMtMUuKGIAzzsWvc8');\" style=\"position:relative;font-size:14px;height:auto;padding:15px 15px 10px 15px;z-index:1;zoom:1;line-height:1.7;\" class=\"body\">\n" +
                "  <div id=\"qm_con_body\">\n" +
                "    <div id=\"mailContentContainer\" class=\"qmbox qm_con_body_content qqmail_webmail_only\" style=\"opacity: 1;\">\n" +
                "      <style type=\"text/css\">\n" +
                "        .qmbox h1,.qmbox \t\t\th2,.qmbox \t\t\th3 {\t\t\t\tcolor: #00785a;\t\t\t}\t\t\t.qmbox p {\t\t\t\tpadding: 0;\t\t\t\tmargin: 0;\t\t\t\tcolor: #333;\t\t\t\tfont-size: 16px;\t\t\t}\t\t\t.qmbox hr {\t\t\t\tbackground-color: #d9d9d9;\t\t\t\tborder: none;\t\t\t\theight: 1px;\t\t\t}\t\t\t.qmbox .eo-link {\t\t\t\tcolor: #0576b9;\t\t\t\ttext-decoration: none;\t\t\t\tcursor: pointer;\t\t\t}\t\t\t.qmbox .eo-link:hover {\t\t\t\tcolor: #3498db;\t\t\t}\t\t\t.qmbox .eo-link:hover {\t\t\t\ttext-decoration: underline;\t\t\t}\t\t\t.qmbox .eo-p-link {\t\t\t\tdisplay: block;\t\t\t\tmargin-top: 20px;\t\t\t\tcolor: #009cff;\t\t\t\ttext-decoration: underline;\t\t\t}\t\t\t.qmbox .p-intro {\t\t\t\tpadding: 30px;\t\t\t}\t\t\t.qmbox .p-code {\t\t\t\tpadding: 0 30px 0 30px;\t\t\t}\t\t\t.qmbox .p-news {\t\t\t\tpadding: 0px 30px 30px 30px;\t\t\t}\n" +
                "      </style>\n" +
                "      <div style=\"max-width:800px;padding-bottom:10px;margin:20px auto 0 auto;\">\n" +
                "        <table cellpadding=\"0\" cellspacing=\"0\" style=\"background-color: #fff;border-collapse: collapse; border:1px solid #e5e5e5;box-shadow: 0 10px 15px rgba(0, 0, 0, 0.05);text-align: left;width: 100%;font-size: 14px;border-spacing: 0;\">\n" +
                "          <tbody>\n" +
                "            <tr style=\"background-color: #f8f8f8;\">\n" +
                "              <td>\n" +
                "                <img style=\"padding: 15px 0 15px 30px;width:50px\" src=\"https://thirdqq.qlogo.cn/g?b=sdk&k=epRXO2ibAGRfYdN2bMv6NKg&kti=Zps0_gAAAAE&s=100&t=1692252333\">" +
                "                <span>coisini技术探索. </span>\n" +
                "              </td>\n" +
                "            </tr>\n" +
                "            <tr>\n" +
                "              <td class=\"p-intro\">\n" +
                "                <h1 style=\"font-size: 26px; font-weight: bold;\">验证您的邮箱地址</h1>\n" +
                "                <p style=\"line-height:1.75em;\">欢迎使用 coisini技术探索： </p>\n" +
                "                <p style=\"line-height:1.75em;\">以下是您的邮箱验证码，请将它输入到 coisini技术探索 的邮箱验证码输入框中:</p>\n" +
                "              </td>\n" +
                "            </tr>\n" +
                "            <tr>\n" +
                "              <td class=\"p-code\">\n" +
                "                <p style=\"color: #253858;text-align:center;line-height:1.75em;background-color: #FDDDC3;min-width: 200px;margin: 0 auto;font-size: 28px;border-radius: 5px;border: 1px solid #d9d9d9;font-weight: bold;\">"+code+"</p>\n" +
                "              </td>\n" +
                "            </tr>\n" +
                "            <tr>\n" +
                "              <td class=\"p-intro\">\n" +
                "                <p style=\"line-height:1.75em;\">这封邮件包括一些您在 coisini技术探索 上的私密账号信息，请不要回复或转发它，以免将自己的信息泄露出去。 </p>\n" +
                "              </td>\n" +
                "            </tr>\n" +
                "            <tr>\n" +
                "              <td class=\"p-intro\">\n" +
                "                <hr>\n" +
                "                <p style=\"text-align: center;line-height:1.75em;\">coisini - <a href='https://www.coisini.cn' style='text-decoration: none;color:#F53F3F'>coisini技术探索</a></p>\n" +
                "              </td>\n" +
                "            </tr>\n" +
                "          </tbody>\n" +
                "        </table>\n" +
                "      </div>\n" +
                "      <style type=\"text/css\">\n" +
                "        .qmbox style, .qmbox script, .qmbox head, .qmbox link, .qmbox meta {display: none !important;}\n" +
                "      </style>\n" +
                "    </div>\n" +
                "  </div><!-- -->\n" +
                "  <style>\n" +
                "    #mailContentContainer .txt {height:auto;}\n" +
                "  </style>\n" +
                "</div></body>\n" +
                "</html>\n";
        try {
            send(email,content);
            log.info("邮箱验证码发送成功,邮箱:{},验证码:{}",email,code);
        } catch (MessagingException e) {
            log.error("邮件发送失败", e);
        }
    }
    private void send(String email, String template) throws MessagingException {

        //创建一个MINE消息
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper mineHelper = new MimeMessageHelper(mimeMessage, true);
        // 设置邮件主题
        mineHelper.setSubject("coisini技术探索");
        // 设置邮件发送者
        mineHelper.setFrom(Objects.requireNonNull(mailProperties.getFrom()));
        // 设置邮件接收者，可以有多个接收者，中间用逗号隔开
        mineHelper.setTo(email);
        // 设置邮件发送日期 获取当前时间
        mineHelper.setSentDate(new Date());
        // 设置邮件的正文
        mineHelper.setText(template,true);
        // 发送邮件
        mailSender.send(mimeMessage);
    }
}
